package com.feng.vhr.controller;

import com.feng.vhr.common.RespBean;
import com.feng.vhr.pojo.*;
import com.feng.vhr.response.REmployee;
import com.feng.vhr.service.*;
import com.feng.vhr.utils.POIUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmpController {
    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private INationService nationService;

    @Autowired
    private IJoblevelService joblevelService;

    @Autowired
    private IPoliticsstatusService politicsstatusService;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IPositionService positionService;

    /**
     * 分页多条件查询员工资料
     * @param page
     * @param size
     * @param emp
     * @return
     */
    @GetMapping("/basic")
    public RespBean page(int page, int size, Employee emp, Date[] beginDateScope) {
        PageHelper.startPage(page,size);
        List<REmployee> allEmpSalaryAndDepartment = employeeService.getAllEmpInfo(emp,beginDateScope);
        PageInfo<REmployee> rEmployeePageInfo = new PageInfo<>(allEmpSalaryAndDepartment);
        return RespBean.ok("员工信息查询成功",rEmployeePageInfo);
    }

    /**
     * 名族列表查询
     * @return
     */
    @GetMapping("/basic/nations")
    public RespBean getNations(){
        List<Nation> list = nationService.list();
        return RespBean.ok("名族列表查询成功",list);
    }

    /**
     * 职称列表查询
     * @return
     */
    @GetMapping("/basic/joblevels")
    public RespBean getJoblevels(){
        List<Joblevel> list = joblevelService.list();
        return RespBean.ok("职称列表查询成功",list);
    }

    /**
     * 政治面貌列表查询
     * @return
     */
    @GetMapping("/basic/politicsstatus")
    public RespBean getPoliticsstatus(){
        List<Politicsstatus> list = politicsstatusService.list();
        return RespBean.ok("政治面貌列表查询成功",list);
    }

    /**
     * 部门列表查询
     * @return
     */
    @GetMapping("/basic/deps")
    public RespBean getDeps(){
        List<Department> list = departmentService.list();
        return RespBean.ok("部门列表查询成功",list);
    }
    /**
     * 职位列表查询
     * @return
     */
    @GetMapping("/basic/positions")
    public RespBean getPositions(){
        List<Position> list = positionService.list();
        return RespBean.ok("职位列表查询成功",list);
    }

    /**
     * 编辑员工资料
     * @param emp
     * @return
     */
    @PutMapping("/basic")
    public RespBean editEmp(@RequestBody Employee emp){
        boolean update = employeeService.updateById(emp);
        if (update){
            return RespBean.ok("编辑员工资料成功");
        }
        return RespBean.error("编辑员工资料失败");
    }

    /**
     * 删除员工资料及账套信息
     * @param id
     * @return
     */
    @DeleteMapping("/basic/{id}")
    @Transactional
    public RespBean deleteEmp(@PathVariable int id){
        boolean b = employeeService.deleteEmpAndSalary(id);
        if (b){
            return RespBean.ok("删除员工资料成功");
        }
        return RespBean.error("删除员工资料失败");
    }

    /**
     * 获取最新工号信息
     * @return
     */
    @GetMapping("/basic/maxWorkID")
    public RespBean maxWorkID(){
        int maxWorkID = employeeService.maxWorkID();
        return RespBean.ok("工号生成成功",String.format("%08d",maxWorkID));
    }

    /**
     * 添加员工资料
     * @param employee
     * @return
     */
    @PostMapping("/basic")
    public RespBean addEmp(@RequestBody Employee employee){
        boolean save = employeeService.save(employee);
        if (save){
            return RespBean.ok("添加员工成功");
        }
        return RespBean.error("添加员工失败");
    }

    /**
     * 员工信息导出
     * @return
     */
    @GetMapping("/basic/export")
    public ResponseEntity<byte[]> export(){
        Employee employee = new Employee();
        List<REmployee> allEmpInfo = employeeService.getAllEmpInfo(employee, null);
        return POIUtil.export(allEmpInfo);
    }

    @PostMapping("/basic/import")
    public RespBean importData(MultipartFile file){
        List<Department> departments = departmentService.list();
        List<Nation> nations = nationService.list();
        List<Position> positions = positionService.list();
        List<Politicsstatus> politicsstatuses = politicsstatusService.list();
        List<Joblevel> joblevels = joblevelService.list();
        List<Employee> employees = POIUtil.importData(file, nations, politicsstatuses, departments, positions, joblevels);
        boolean b = employeeService.saveBatch(employees);
        if (b){
            return RespBean.ok("导入数据成功");
        }
        return RespBean.error("导入数据失败");
    }

}
